/*
 * Created on Sat Sep 16 2017
 * @flow
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

import * as HttpMethod from '../constants/HttpMethod';
import * as ResponseType from '../constants/ResponseType';
import URI from 'urijs';

export default class NetRequest {
  url: string;
  urlParam: Object;
  body: any;
  headers: Object;
  method: string;
  responseType: string;

  constructor(url: string,
    urlParam: ?Object,
    body: any,
    headers: ?Object,
    method: string = HttpMethod.GET,
    responseType: string = ResponseType.JSON_TYPE) {

    this.url = url;
    this.urlParam = urlParam || {};
    this.body = body || null;
    this.headers = headers || {};
    this.method = method;
    this.responseType = responseType;
  }

  clone(): NetRequest {
    let request = new NetRequest(this.url,
      this.urlParam,
      this.body,
      this.headers,
      this.method,
      this.responseType);
    return request;
  }

  get requestUrl(): string {
    let uri = new URI(this.url);
    if (this.urlParam && Object.keys(this.urlParam).length) {
      uri.setSearch(this.urlParam);
    }

    return uri.toString();
  }
}